package de.openkanban.client.ui.footer;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

import de.openkanban.client.datasource.UserContactDS;
import de.openkanban.client.ui.presenter.DeleteUserFromContactsBooleanCallback;

/**
 * GUI Element of the UserContactsListGrid.
 */
public class UserContactsListGrid extends ListGrid {

	public UserContactsListGrid() {
		setWidth100();
		setHeight(146);
		setDataSource(UserContactDS.getInstance());
		setAutoFetchData(true);
		setCanDragRecordsOut(true);
		setCanAcceptDroppedRecords(false);
		setCanReorderRecords(true);
		setPreventDuplicates(true);

		addDoubleClickHandler(new DoubleClickHandler() {
			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				ListGrid bctg = (ListGrid) event.getSource();
				ListGridRecord tr = bctg.getSelectedRecord();
				if (tr != null) {
					String vorname = tr.getAttribute("vorname");
					String nachname = tr.getAttribute("nachname");
					SC.confirm("Do you want to remove " + vorname + " " + nachname + " from your Contacts?",
							new DeleteUserFromContactsBooleanCallback(event.getSource()));
				}
			}
		});

		ListGridField avatarUrl = new ListGridField("avatarUrl", " ", 35);
		ListGridField vorname = new ListGridField("vorname", "First Name");
		ListGridField nachname = new ListGridField("nachname", "Last Name");
		ListGridField email = new ListGridField("email", "E-Mail");
		ListGridField namensKuerzel = new ListGridField("namensKuerzel", " ", 50);
		avatarUrl.setAlign(Alignment.CENTER);
		namensKuerzel.setAlign(Alignment.CENTER);
		setFields(avatarUrl, vorname, nachname, email, namensKuerzel);
	}

}
